#include "extend_color.h"
#include <stdio.h>

// 默认颜色定义
const STColor COLOR_RED = {230, 0, 0, 230};
const STColor COLOR_GREEN = {0, 230, 0, 230};
const STColor COLOR_BLUE = {0, 0, 230, 230};
const STColor COLOR_WHITE = {230, 230, 230, 230};
const STColor COLOR_BLACK = {0, 0, 0, 230};
const STColor COLOR_YELLOW = {230, 230, 0, 230};
const STColor COLOR_CYAN = {0, 230, 230, 230};
const STColor COLOR_MAGENTA = {230, 0, 230, 230};

// 辅助函数：设置背景色
void set_bg_color(const STColor *color) {
    printf("\x1b[48;2;%d;%d;%dm", color->red, color->green, color->blue);
}

// 辅助函数：设置前景色
void set_fg_color(const STColor *color) {
    printf("\x1b[38;2;%d;%d;%dm", color->red, color->green, color->blue);
}

// 辅助函数：重置所有颜色
void reset_colors(void) {
    printf("\x1b[0m");  // 使用更简洁的ANSI重置指令
}

// 输出纯背景色的彩色方块
void print_colored_square(const STColor *color) {
    set_bg_color(color);
    printf("  ");        // 使用两个空格字符显示更明显的方块
    reset_colors();
}

// 输出自定义字符（允许单独控制前景/背景色）
void print_colored_character(const STColor *fg_color, const STColor *bg_color, const char *character) {
    set_bg_color(bg_color);
    set_fg_color(fg_color);
    printf("%s", character);
    reset_colors();
}

// 输出彩色字符串
void print_colored_string(const STColor *color, const char *text) {
    set_fg_color(color);
    printf("%s", text);
    reset_colors();
}

int mainColorTest() {
    // 使用默认颜色打印彩色方块
    print_colored_square(&COLOR_RED);
    print_colored_square(&COLOR_GREEN);
    print_colored_square(&COLOR_BLUE);
    printf("\n");

    // 使用默认颜色打印彩色字符串
    print_colored_string(&COLOR_WHITE, "Hello, ");
    print_colored_string(&COLOR_BLUE, "World!\n");

    // 使用默认颜色打印自定义字符
    print_colored_character(&COLOR_WHITE, &COLOR_BLACK, "A");
    print_colored_character(&COLOR_RED, &COLOR_GREEN, "B");
    print_colored_character(&COLOR_BLUE, &COLOR_WHITE, "C");
    printf("\n");

    return 0;
}

// Function to convert STColor (pointer) to uint32_t
uint32_t stcolor_to_u32(const STColor* color) {
    if (color == NULL) {
        fprintf(stderr, "Error: Null pointer passed to stcolor_to_u32\n");
        return 0; // Return 0 or handle the error appropriately
    }
    uint32_t result = 0;
    result |= (uint32_t)color->alpha << 24; // Shift alpha to the highest 8 bits
    result |= (uint32_t)color->red  << 16; // Shift red to the next 8 bits
    result |= (uint32_t)color->green << 8;  // Shift green to the next 8 bits
    result |= (uint32_t)color->blue;         // Blue stays in the lowest 8 bits
    return result;
}

// Function to convert uint32_t to STColor (pointer)
void u32_to_stcolor(uint32_t color, STColor* result) {
    if (result == NULL) {
        fprintf(stderr, "Error: Null pointer passed to u32_to_stcolor\n");
        return; // Handle the error appropriately
    }
    result->alpha = (uint8_t)((color >> 24) & 0xFF); // Extract alpha from the highest 8 bits
    result->blue  = (uint8_t)((color >> 16) & 0xFF); // Extract blue from the next 8 bits
    result->green = (uint8_t)((color >> 8)  & 0xFF); // Extract green from the next 8 bits
    result->red   = (uint8_t)(color & 0xFF);         // Extract red from the lowest 8 bits
}
